<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" type="text/css" href="css/mainstyle.css" />
    <link rel="icon" href="image/logo.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="css/radiocheck.css" />
    <link rel="stylesheet" type="text/css" href="css/button.css" />
    <title>疫苗预约</title>
    <style type="text/css">
    .clearfix:after {
        content: ".";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
    }
        #main_body_date,#main_body_type,#main_body_man,#main_body_time,#main_body_submit{
            width: 90%; 
            margin-left: 5%;
            float: left;
        }
        #main_body_man{
            height: 10rem;
        }
        #main_body_date_text1{
            line-height: 5rem;
            font-weight: bold;
            float: left;
        }
        #main_body_date_text{
            line-height: 3.2rem;
            padding-top: 0.8rem;
            font-weight: bold;
            float: left;
        }
        #main_body_date_lump{
            width: 3rem;
            height: 5rem;
            color: #615c5c;
            float: left;
            font-size: 1rem;
            margin-left: 1%;
        }
        #lump_text1,#lump_text2,#lump_week,.lump_date{
            width: 100%;
            height: 1.65rem;
            margin: 0;
            text-align: center;
            padding-top: 0.35rem;
        }
        #lump_week{
            color: black;
        }
        #lump_text2,.lump_date{
            height: 2.2rem;
            padding-top: 0.8rem;
            
        }
        /* #lump_date{
            background-color: rgb(100 244 175); 
            background:url(image/onclick.png) no-repeat;
            background-size:48px 48px; 
            color: black;
        } */
        #main_body_type_input{
            height: 2.7rem;
            padding-top: 1.3rem;

        }
        #type_input1{
            margin-top: 0.5rem;
            margin-right: 2rem; 
            float: left;
        }
        #main_body_submit_button{
            height: 3rem;
            float: auto;
            margin-left: auto;
            padding-left: 42%;
        }
        #TdateId{
            width: 0px;
            height: 0px;
        }
        p{
            cursor:pointer;
        }
    </style>
    <?php
        @session_start();
        $idUser=$_SESSION['idUser'];
        $userName=$_SESSION['userName'];//用户名
        $headImgSrc=$_SESSION['imgsrc'];//头像路径

        $dateNumber=15;//日期块个数
        $dateArr=array();//时间数组用于显示可选择的预约时间
        $weekArr=array('日','一','二','三','四','五','六');
        $today=getdate();//获取当天时间
        $year=$today['year'];
        /* $mon=$today['mon'];//月
        $mday=$today['mday'];//日
        $tomorrow=$mday+1;//明天 */
        $weekday=$today['wday']+1;//星期（数字 明天）
        for($i=0;$i<$dateNumber;$i++){
            $tmp=$i+1;
            $dateArr[0][$i]=date("m-d",strtotime("+$tmp day"));//存日期
            $dateArr[1][$i]=$weekArr[$weekday++];
            if($weekday==7)
                $weekday=0;
        }

        include_once 'api/DBcon.php';
        $result=false;
        $link=OpenDB();
        @session_start();
        $userid=$_SESSION['idUser'];

        if($link){
            $query2="select * from information where userid='${userid}';";//查询账号内成员
            $result2 = mysqli_query($link,$query2);
            $familyNum=mysqli_num_rows($result2);//有成员
            if($familyNum) { //存在
                $rows = mysqli_fetch_all($result2);//读取结果
            }
            else{
                echo "<script>alert('数据库出错！');</script>";
            }
        }
        function errreturn(string $err){//信息重复时
            echo "<script>parent.location.href='main_reservation.php';alert('".$err."');</script>";
        }
        function yes2(string $err){//信息插入成功时返回家人页面
            echo "<script>parent.location.href='main_family.php';alert('".$err."');</script>";
        }
        if(isset($_POST['submit'])&&$link){
            //首先判空
            if($_POST['Tdate']==""||$_POST['Tdate']==null)
                errreturn("忘记选择日期了哦！");
            if($_POST['time']==""||$_POST['time']==null)
                errreturn("忘记选择时间了哦！");
            if($_POST['state']==""||$_POST['state']==null)
                errreturn("忘记选择疫苗类型了哦！");
            if($_POST['Tname']==""||$_POST['Tname']==null)
                errreturn("忘记选择接种人员了哦！");

            $Tdate=$year."-".$_POST['Tdate'];//提交的日期连接上年份
            $Ttime=$_POST['time'];//提交的时间
            $Ttype=$_POST['state'];//提交的疫苗类型
            $Tid=$_POST['Tname'];//提交的人员id

            $Tnum=count($Tid);
            if($Ttype=="ready1"){//判断预约的疫苗类型以存入不同的时间列中
                $reserdate="reserdate1";
                $resertime="resertime1";
            }
            else{
                $reserdate="reserdate2";
                $resertime="resertime2";
            }
            
            $number=0;
            //
            for($j=0;$j<$Tnum;$j++){
                $query3="UPDATE information SET state='${Ttype}',${reserdate}='${Tdate}',${resertime}='${Ttime}' WHERE id='"."$Tid[$j]"."';";
                $result = mysqli_query($link,$query3);
                if(mysqli_affected_rows($link))
                    $number+=1;
            }
            if($number) { 
                    yes2("共${number}人预约成功！");
            }
            else{
                errreturn("预约失败！");
            } 
        }
    ?>
</head>
<body>
    <div id="left_part">
    <div id="navigationBar">
        <div id=logo>
            <img src="image/logo1.png" title="疫苗预约" id="logoimg"/>
            <img src="image/text.png" title="yuyue" id="logotext"/>
        </div>
        <div id="buttons">
            <a href="main_family.php">
            <div class="button" id="family" onmouseover="this.style.background='#84858d14'" onmouseout="this.style.background=''">
                <img src="image/jiaren.png" title="我的家人" id="icon"/>
                我的家人
            </div>
            </a>
            
            <div class="button1" id="reservation" >
                <img src="image/yuyue.png" title="预约注射" id="icon"/>
                预约注射
            </div>
           
            <a href="main_notice.php">
            <div class="button" id="notice" onmouseover="this.style.background='#84858d14'" onmouseout="this.style.background=''">
                <img src="image/zhuyi.png" title="注意事项" id="icon"/>
                注意事项
            </div> </a>
        </div>   
    </div>
    <div id="user">
        <img src="<?php echo $headImgSrc;?>" title="头像" id="headimg"/>
        <div id="userId"><?php echo $userName;?></div>
        <a href="updateuser.php"><img src="image/dangan.png" title="信息" id="myId"/></a>
        
    </div>
    <a href="index.php"><div id="bottom">退出登录</div></a>
</div>
    <div id="main">
        <div id="main_head">
            <div id="main_head_text">预约注射></div>
            <div id="main_head_imgb"><img src="image/wenhua.jpeg" title="添加" id="main_head_icon"/></div>
        </div>
        <div id="main_body">
            <form action="" method="post">
                <input type="text" name="Tdate" id="TdateId" visibility = "visible" value="111" />
            <div id="main_body_date" class="clearfix">
                <div id="main_body_date_text1">选择日期：</div>
                <div id="main_body_date_lump" value="18">
                    <div id="lump_text1">星期</div>
                    <div id="lump_text2">日期</div>
                </div>
                <?php
                for($j=0;$j<$dateNumber;$j++){
                    echo "<div id=\"main_body_date_lump\">
                    <div id=\"lump_week\">".$dateArr[1][$j]."</div>                   
                    <p id=\"".$dateArr[0][$j]."\" class=\"lump_date\" onselectstart=\"return false;\">".$dateArr[0][$j]."</p>
                    </div>";
                }
                ?>

            </div>

            <div id="main_body_time">
                <div id="main_body_date_text">选择时间：</div>
                <div id="main_body_type_input">
                    <div id="type_input1"><input type="radio" name="time" class="gcs-radio" id="am" value="上午"/>
                    <label for="am"></label><label for="am" >上午</label></div>
                    <div id="type_input1"><input type="radio" name="time" class="gcs-radio" id="pm" value="下午"/>
                    <label for="pm"></label><label for="pm">下午</label></div>
                </div>
            </div>

            <div id="main_body_type">
                <div id="main_body_date_text">疫苗类型：</div>
                <div id="main_body_type_input">
                    <div id="type_input1"><input type="radio" name="state" class="gcs-radio" id="state1" value="ready1"/>
                    <label for="state1"></label><label for="state1" >新冠第一针</label></div>
                    <div id="type_input1"><input type="radio" name="state" class="gcs-radio" id="state2" value="ready2"/>
                    <label for="state2"></label><label for="state2">新冠第二针</label></div>
                </div>
            </div>

            <div id="main_body_man">
                <div id="main_body_date_text">接种人员：</div>
                <div id="main_body_type_input">
                    <?php 
                        for($i=0;$i<$familyNum;$i++){
                            echo "
                            <div id=\"type_input1\"><input type=\"checkbox\" name=\"Tname[]\" class=\"gcs-checkbox\" id=\"".$rows[$i][0]."\" 
                            value=\"".$rows[$i][0]."\"/>
                            <label for=\"".$rows[$i][0]."\"></label><label for=\"".$rows[$i][0]."\">".$rows[$i][1]."</label></div>";
                        }
                    ?>
                </div>
            </div>

            <div id="main_body_submit">
                    <div id="main_body_submit_button">
                        <button type="submit" name="submit" id="submit" value="resubmit" class="buttons green">提交预约</button>
                    </div>
            </div>
        </form>
        </div>
        <div id="main_bottom">
            <span>地址：武汉市东湖新技术开发区文华园路8号</span>
            <span>作者：文华学院信息学部计科1806刘昊天</span>
        </div>
    </div>
    <script type="text/javascript">
        var Tdate = document.getElementById("TdateId");//取到存日期的输入框
        Tdate.style.visibility = "hidden";//将其隐形

        window.onload = function(){
        var divs = document.getElementsByTagName("p");//取所有p标签（即所有日期方块）
        var len = divs.length;//统计个数
        for(var i=0;i<len;i++){
            divs[i].onclick = function(){//点击某日期标签时
                for(var j=0;j<len;j++){//先将所有日期方块样式设为正常
                    divs[j].style.background = "";
                    divs[j].style.color = "#615c5c";
                    divs[j].style.fontWeight = "normal";
                }
                //将点击的方块着重显示
                this.style.background = "#64f4af";
                this.style.color = "black";
                this.style.fontWeight = "bolder";
                Tdate.value=this.id;//把点击的方块的id值赋给存放日期的隐形输入框
            };
        }
        };
    </script>
</body>
</html>